/******************************************************************************

    Copyright (C) 2006 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.engine.syspreds;

import java.util.List;
import java.util.Vector;

import prettyprolog.datatypes.Callable;
import prettyprolog.datatypes.Constant;
import prettyprolog.datatypes.Real;
import prettyprolog.datatypes.Substitution;
import prettyprolog.datatypes.Term;
import prettyprolog.datatypes.Variable;
import prettyprolog.engine.Domain;
import prettyprolog.engine.Engine;
import prettyprolog.engine.Function;
import prettyprolog.engine.FunctionException;
import prettyprolog.engine.UnificationAlgorithm;

/**
 * Implements the "#\=" system predicate.
 */
public class MinoreUg extends SystemPredicate {

	private static final MinoreUg inst = new MinoreUg();

	private MinoreUg() {
		super(2);
	}

	/** Returns the only instance of this system predicate. */
	public static MinoreUg getInstance() {
		return inst;
	}

	public SyspredEffect doCall(Engine en, Substitution subst, List<Term> args) throws SyspredException {
		Vector temp = new Vector();
		VariableCount(args, temp);
		Vector a = subst.getVarList();
		boolean flag1 = true;
		for(int i=0; i < temp.size(); i++) {
			if(a.contains(temp.get(i))) {
				flag1 = true;
			}
		}
		boolean risp = false;
		if(flag1) {
			Vector Adom = en.getStack().get(0).getArcDomain();
			Vector Bdom = en.getStack().get(0).getBoundDomain();
			Substitution sub = en.getStack().get(0).getSubstitution();
			risp = en.Solve(en, args, temp, sub, "#=<", Adom, Bdom);
			en.getStack().get(0).setArcDomain(Adom);
			en.getStack().get(0).setBoundDomain(Bdom);
			if(risp) {
				return new SyspredEffect(false, subst);
			} else 
			return new SyspredEffect(true, subst);
		} else {
			return new SyspredEffect(false, subst);
		}
	}

    public void VariableCount(List<Term> ls, Vector<Variable> V) {
//      List<Term> ls = c.getArgs();
      for(Term t : ls) {
          if (t instanceof Variable) {
              if(!V.contains((Variable) t)) {
                  V.add((Variable) ((Variable) t));
              }
          } else if (t instanceof Callable) {
          	List<Term> args = ((Callable) t).getArgs();
              VariableCount(args, V);
          }
      }
  }	
	
	public boolean isBacktrackable() {
		return false;
	}

	public String toString() {
		return super.toString() + " #=<";
	}
}